package com.flyco.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.modules.system.entity.SysUser;
import com.flyco.modules.system.entity.SysUserRelation;
import com.flyco.modules.system.mapper.SysUserRelationMapper;
import com.flyco.modules.system.service.ISysUserRelationService;
import com.flyco.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 人员关系
 * @Author: flyco
 * @Date: 2023-07-13
 * @Version: V1.0
 */
@Service
public class SysUserRelationServiceImpl extends ServiceImpl<SysUserRelationMapper, SysUserRelation> implements ISysUserRelationService {

    @Autowired
    private SysUserRelationMapper userRelationMapper;
    @Autowired
    private ISysUserService userService;

    @Override
    public List<SysUser> getByParentUserId(Long parentUserId) {

        LambdaQueryWrapper<SysUserRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRelation::getParentUserId, parentUserId)
                .eq(SysUserRelation::getStatus, 1);

        List<SysUserRelation> userRelationList = userRelationMapper.selectList(wrapper);

        if (!CollectionUtils.isEmpty(userRelationList)) {
            return userService.getByUserIds(userRelationList.stream().map(o -> o.getParentUserId()).collect(Collectors.toList()));
        }
        return null;
    }

    @Override
    public List<SysUserRelation> getRelationByParentUserId(Long parentUserId) {

        LambdaQueryWrapper<SysUserRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRelation::getParentUserId, parentUserId)
                .eq(SysUserRelation::getStatus, 1);

        List<SysUserRelation> userRelationList = userRelationMapper.selectList(wrapper);
        return userRelationList;
    }
}
